/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.app.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xw.app.model.AcctRole;
import com.xw.app.service.AcctRoleService;
import com.xw.app.vo.query.AcctRoleQuery;
import com.xw.framework.base.controller.BaseSpringController;
import com.xw.util.BeanUtils;

/**
 * The Class AcctRoleController.
 *
 * @author Xi Wei
 */
@Controller
@RequestMapping("/acctRole")
public class AcctRoleController extends BaseSpringController {
	
	@Autowired
	private AcctRoleService acctRoleService;
	
	/**
	 * List.
	 *
	 * @param request the request
	 * @param query the query
	 * @return the model and view
	 */
	@RequestMapping("/list")
	public ModelAndView list(HttpServletRequest request,AcctRoleQuery query){
		Page<AcctRole> page = acctRoleService.findPage(query);
		request.getSession().setAttribute(AcctRoleQuery.class.getSimpleName(), query);
		ModelAndView result = new ModelAndView("/AcctRole/list");
		ModelMap map = toModelMap(page, query);
		result.addAllObjects(map);
		return result;		
	}
	
	/**
	 * Creates the.
	 *
	 * @param acctRole the acct role
	 * @return the model and view
	 */
	@RequestMapping("/popup/create")
	public ModelAndView create(AcctRole acctRole){
		ModelAndView result = new ModelAndView("/AcctRole/add");
		ModelMap map = new ModelMap();
		if(acctRole.getId() != null){
			acctRole = acctRoleService.getById(acctRole.getId());
		}
		map.addAttribute("acctRole", acctRole);
		result.addAllObjects(map);
		return result;
	}
	
	
	/**
	 * Save.
	 *
	 * @param acctRole the acct role
	 * @param validateResult the validate result
	 * @param request the request
	 * @return the model and view
	 */
	@RequestMapping("/popup/save")
	public ModelAndView save(@Valid AcctRole acctRole,BindingResult validateResult,HttpServletRequest request){
		if(validateResult.hasErrors()){
			return create(acctRole);
		}
		
		if(acctRole.getId() != null){
			AcctRole existing = acctRoleService.getById(acctRole.getId());
			existing.setRoleCode(acctRole.getRoleCode());
			existing.setRoleName(acctRole.getRoleName());
			existing.setRoleDesc(acctRole.getRoleDesc());
			acctRoleService.save(existing);			
		}else{
			acctRoleService.save(acctRole);
		}
		
		ModelMap map = new ModelMap();
		ModelAndView result = new ModelAndView("/saveResult");
		map.addAttribute("msg", "success");
		result.addAllObjects(map);
		return result;
	}
	
	/**
	 * Delete.
	 *
	 * @param id the id
	 * @return the string
	 */
	@RequestMapping("/json/delete")
	@ResponseBody
	public String delete(Long id){
		acctRoleService.delete(id);
		return "success";
		
	}
	
	/**
	 * Delete all.
	 *
	 * @param ids the ids
	 * @return the string
	 */
	@RequestMapping("/json/deleteAll")
	@ResponseBody
	public String deleteAll(String ids){
		System.out.println(ids);
		if(ids != null && ids.trim().length() > 0){
			Arrays.asList(ids.split(",")).forEach(x -> acctRoleService.delete(Long.valueOf(x)));
		}
		return "success";
	}
	
	/**
	 * Check code.
	 *
	 * @param roleCode the role code
	 * @param id the id
	 * @return the string
	 */
	@RequestMapping("/json/checkCode")
	@ResponseBody
	public String checkCode(String roleCode,Long id){
		if(acctRoleService.checkRoleExist(roleCode,id)){
			return "false";
		}
		return "true";
	}
	
	/**
	 * Gets the role all.
	 *
	 * @return the role all
	 */
	@RequestMapping("/getRoleAll")
	@ResponseBody
	public List<AcctRole> getRoleAll(){
		return acctRoleService.findAll();
	}

}
